var t = getApp();

Component({
    properties: {},
    data: {
        popShow: !1,
        username: "",
        mobile: "",
        address: "",
        province: "",
        city: "",
        district: "",
        street: "",
        addressName: "",
        districtVal: "",
        strShow: !1,
        provinceData: [],
        pickerValue: [ 0, 0, 0 ],
        province_id: 0,
        city_id: 0,
        district_id: 0,
        street_id: 0
    },
    lifetimes: {
        attached: function() {
            this._initAddressInfo();
        }
    },
    methods: {
        setPopShow: function() {
            this.setData({
                popShow: !this.data.popShow
            });
        },
        setFormData: function(t) {
            var e = {
                street: "",
                districtVal: ""
            };
            e.addressName = t.province + " " + t.city + " " + t.district, e.province = t.province, 
            e.city = t.city, e.district = t.district, e.street = t.street, e.address = t.address, 
            e.username = t.realname, e.mobile = t.mobile, e.province_id = t.province_id, e.city_id = t.city_id, 
            e.district_id = t.district_id, e.street_id = t.street_id, this.setData(e), this.callback(), 
            this._getStreet(t.district_id);
        },
        _initAddressInfo: function() {
            var e = this;
            wx.getStorage({
                key: "provinceData",
                success: function(t) {
                    console.log("地址信息准备就绪"), e._getProvinceData();
                },
                fail: function(a) {
                    console.log("省fail"), t._getNetWork({
                        url: t.getNetAddresss("member.member-address.address"),
                        success: function(t) {
                            var a = t.data, s = a.data.province, i = a.data.city, d = a.data.district;
                            wx.setStorage({
                                key: "provinceData",
                                data: s
                            }), wx.setStorage({
                                key: "cityData",
                                data: i
                            }), wx.setStorage({
                                key: "districtData",
                                data: d
                            }), e._initAddressInfo();
                        },
                        fail: function(t) {},
                        state: !0
                    });
                }
            });
        },
        _getProvinceData: function() {
            var t = this;
            wx.getStorage({
                key: "provinceData",
                success: function(e) {
                    t.setData({
                        provinceData: e.data
                    }), t.setData({
                        "selectAddressData[0].name": e.data[t.data.pickerValue[0]].areaname,
                        "selectAddressData[0].id": e.data[t.data.pickerValue[0]].id,
                        "selectAddressData[0].index": t.data.pickerValue[0]
                    }), t._getCityData();
                }
            });
        },
        _getCityData: function() {
            var t = this.data.selectAddressData[0], e = this;
            wx.getStorage({
                key: "cityData",
                success: function(a) {
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                    e.setData({
                        cityData: s
                    }), e.setData({
                        "selectAddressData[1].name": s[e.data.pickerValue[1]].areaname,
                        "selectAddressData[1].id": s[e.data.pickerValue[1]].id,
                        "selectAddressData[1].index": e.data.pickerValue[1]
                    }), e._getDistrictData();
                }
            });
        },
        _getDistrictData: function() {
            var t = this.data.selectAddressData[1], e = this;
            wx.getStorage({
                key: "districtData",
                success: function(a) {
                    console.log("地区读取成功");
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                    e.setData({
                        districtData: s
                    }), e.setData({
                        "selectAddressData[2].name": s[e.data.pickerValue[2]].areaname,
                        "selectAddressData[2].id": s[e.data.pickerValue[2]].id,
                        "selectAddressData[2].index": e.data.pickerValue[2]
                    });
                }
            });
        },
        openDateLwtbtn: function() {
            this.setData({
                openDateLw: !0
            });
        },
        _closeDateLw: function() {
            this.setData({
                openDateLw: !1
            });
        },
        bindChange: function(t) {
            var e = t.detail.value;
            this.data.pickerValue[0] != e[0] && (e[1] = 0, e[2] = 0), this.data.pickerValue[1] != e[1] && (e[2] = 0), 
            this.setData({
                "pickerValue[0]": e[0],
                "pickerValue[1]": e[1],
                "pickerValue[2]": e[2]
            }), this._initAddressInfo();
        },
        _okAddress: function() {
            var t = this.data.selectAddressData;
            this.setData({
                street: "",
                districtVal: "",
                addressName: t[0].name + " " + t[1].name + " " + t[2].name,
                province: t[0].name,
                city: t[1].name,
                district: t[2].name,
                province_id: t[0].id,
                city_id: t[1].id,
                district_id: t[2].id
            }), this._getStreet(t[2].id), this._closeDateLw();
        },
        _getStreet: function(e) {
            var a = this, s = t.getNetAddresss("member.memberAddress.street");
            s += "&district_id=" + e, t._getNetWork({
                url: s,
                success: function(e) {
                    var s = e.data;
                    1 != s.result || t._isTextEmpty(s.data) ? a.setData({
                        strShow: !1
                    }) : !t._isTextEmpty(s.data) && s.data.length > 0 ? a.setData({
                        districtVal: s.data,
                        strShow: !0
                    }) : a.setData({
                        strShow: !1
                    });
                },
                fail: function(t) {
                    a.setData({
                        strShow: !1
                    }), console.log(t);
                }
            });
        },
        streetChoose: function() {
            t._isTextEmpty(this.data.addressName) ? wx.showToast({
                icon: "none",
                title: "请先选择所在地区",
                duration: 1500
            }) : this.setData({
                streetShow: !0,
                opren: !1
            });
        },
        streetClose: function() {
            this.setData({
                streetShow: !1,
                opren: !0
            });
        },
        streetConfirm: function(t) {
            var e = t.target.dataset.item;
            this.setData({
                street: e.areaname,
                street_id: e.id,
                streetShow: !1,
                opren: !0
            });
        },
        appendAddress: function() {
            return t._isTextEmpty(this.data.username) ? t.tips("请输入收货人姓名") : t._isTextEmpty(this.data.mobile) ? t.tips("请输入联系电话") : /^[0-9]{1,}$/.test(this.data.mobile) ? t._isTextEmpty(this.data.addressName) ? t.tips("请选择所在区域") : this.data.strShow && t._isTextEmpty(this.data.street) ? t.tips("请选择所在街道") : t._isTextEmpty(this.data.address) ? t.tips("请输入详细地址") : (this.callback(), 
            void this.setPopShow()) : t.tips("请输入联系电话");
        },
        callback: function() {
            var t = {
                pick_up_name: this.data.username,
                pick_up_tel: this.data.mobile,
                pick_up_province: this.data.province_id,
                pick_up_city: this.data.city_id,
                pick_up_district: this.data.district_id,
                pick_up_street: this.data.street_id,
                pick_up_address: this.data.address,
                street: this.data.street || "",
                addressName: this.data.addressName
            };
            this.triggerEvent("confirm", t);
        }
    }
});